package ipower.studentworks.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;

import ipower.studentworks.dao.UnitDao;
import ipower.studentworks.domain.Unit;
import ipower.studentworks.domain.UnitBase;
import ipower.studentworks.pageModel.UnitInfo;
import ipower.studentworks.service.UnitService;
/**
 * 学校单位服务实现。
 * @author young。
 * @since 2013-11-03.
 * */
public class UnitServiceImpl extends DataServiceImpl<Unit, UnitInfo> implements UnitService {
	private static final long serialVersionUID = 1L;
	private UnitDao dao;
	@Override
	public void setUnitDao(UnitDao unitDao) {
		this.dao = unitDao;
	}
	@Override
	protected List<Unit> find(UnitInfo info) {
		String hql = "from Unit a where 1 = 1 ";
		Map<String, Object> parameters = new HashMap<>();
		hql = this.addWhere(info, hql, parameters);
		if(info.getSort() != null && !info.getSort().isEmpty() && info.getOrder() != null && !info.getOrder().isEmpty()){
			hql += " order by a."+ info.getSort()+ " "+ info.getOrder();
		}
		return this.dao.find(hql, parameters, info.getPage(), info.getRows());
	}
	@Override
	protected UnitInfo changeModel(Unit data) {
		UnitInfo info = new UnitInfo();
		BeanUtils.copyProperties(data, info);
		return info;
	}
	@Override
	protected Long total(UnitInfo info) {
		String hql = "select count(*) from Unit a where 1 = 1 ";
		Map<String, Object> parameters = new HashMap<>();
		hql = this.addWhere(info, hql, parameters);
		return this.dao.count(hql, parameters);
	}
	@Override
	protected String addWhere(UnitInfo info, String hql, Map<String, Object> parameters) {
		if(info.getName() != null && !info.getName().isEmpty()){
			hql += " and (a.name like :name or a.code like :name)";
			parameters.put("name", "%"+ info.getName()+"%");
		}
		return hql;
	}
	@Override
	public UnitInfo update(UnitInfo info) {
		if(info != null){
			boolean isAdd = false;
			Unit data = (info.getId() == null || info.getId().isEmpty()) ? null : this.dao.load(Unit.class, info.getId());
			if(data == null){
				info.setId(UUID.randomUUID().toString());
				isAdd = true;
				data = new Unit();
				info.setCreate(new Date());
				info.setModify(null);
			}else {
				info.setCreate(data.getCreate());
				info.setModify(new Date());
			}
			BeanUtils.copyProperties(info, data);
			if(isAdd) this.dao.save(data);
		}
		return info;
	}
	@Override
	public void delete(String[] ids) {
		if(ids == null || ids.length == 0) return;
		for(String id : ids){
			if(id == null || id.isEmpty()) continue;
			Unit data = this.dao.load(Unit.class, id);
			if(data != null){
				this.dao.delete(data);
			}
		}
	}
	@Override
	public Unit loadUnit(String id) {
		if (id != null && !id.isEmpty()) {
			return this.dao.load(Unit.class, id);
		}
		return null;
	}
	@Override
	public List<UnitBase> loadAllUnits(String name) {
		String hql = "from Unit u ";
		Map<String, Object> parameters = new HashMap<>();
		if(name != null && !name.isEmpty()){
			hql +=" where (u.code like :name or u.name like :name)";
			parameters.put("name", "%"+ name +"%");
		}
		List<Unit> list = this.dao.find(hql, parameters, null, null);
		if(list != null && list.size() > 0){
			List<UnitBase> results = new ArrayList<>();
			for(Unit u : list){
				results.add(u);
			}
			return results;
		}
		return null;
	}
}